Rust Remove SafeSliceAccess for Arrays, and fix miri.#6592
Merged
CasperN merged 2 commits intogoogle:masterfrom Apr 26, 2021
Merged
Rust Remove SafeSliceAccess for Arrays, and fix miri.#6592CasperN merged 2 commits intogoogle:masterfrom
CasperN merged 2 commits intogoogle:masterfrom
Conversation
added 2 commits
April 26, 2021 13:36
SafeSliceAccess was removed for Arrays. It's kind of unsound. It has two properties: 1. EndianSafe 2. Alignment 1 We only need 1. in create_vector_direct to memcpy data. We both 1. and 2. for accessing things with slices as buffers are built on &[u8] which is unaligned. Conditional compilation implements SafeSliceAccess for >1byte scalars (like f32) on LittleEndian machines which is wrong since they don't satisfy 2. This UB is still accessible for Vectors (though not exercised our tests) as it implements SafeSliceAccess. I'll fix this later by splitting SafeSliceAccess into its 2 properties.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resubmitting #6583 and fixing associated problems.
Looks like the Miri test in
RustTest.shdidn't catch errors in #6548 related to the unsoundness ofSafeSliceAccess.I removed
SafeSliceAccessfor Arrays, and probably should do so for Vectors in the future too.Calling
safe_slice() -> &[T]requiresTto bealign_of::<T>() == 1.I actually almost saw this error in #6548 but figured it was fine because I thought SafeSliceAccess was only implemented in endian-safe, alignment-1, types; u8, i8, bool, and flatbuffers structs; however we do conditional compilation that adds SafeSliceAccess to larger scalars on little-endian targets... 🤦 This is just wrong and callers of
safe_slice()may get alignment errors.I left in the conditional compilation since
create_vector_directdepends onSafeSliceAccessbut only requires property 1 (its doing memcpy, no dereferencing). We test and advertise that function for vectors of larger scalars so people may be using it.create_vector_directis fine but if clients useflatbuffers::Vector<f32>::safe_slice(), that triggers UB.In a future PR, I will split SafeSliceAccess into the two properties to deprecate the conditional compilation and make things safe.
@3ddi @krojew